<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <div id="app">
        <input type="text" v-focus="false">
        <input type="text" v-focus="true">
        <h1 v-myif="flag">你好</h1>
        <button @click="say()">点击</button>
    </div>
    <script src=" ./vue.js "></script>
    <script>
        // Vue的自定义指令
        // v-if v-show v-for v-model v-bind v-on v-html v-text 
        // 自定义全局指令
        Vue.directive('focus', {
            bind(el, binding) { //只调用一次，指令第一次绑定到元素时调用
            },
            inserted(el, binding, vnode) { //绑定指令的元素 添加到页面中
                // el---当前元素
                // binding
                // .name   指令名  focus
                // .value  指令后的值
                // .oldVal 原来的值
                if (binding.value) {
                    el.focus();
                    el.style.background = "blue ";
                }
            }
        });

        new Vue({
            el: "#app ",
            data() {
                return {
                    flag: true,
                    eleparent: ""
                }
            },
            methods: {
                say() {
                    this.flag = !this.flag;
                }
            },
            // 局部指令
            directives: {
                myif: {
                    inserted(el, binding, vnode) {
                        console.log(vnode)
                            // console.log(binding.value)
                        console.log(el.parentNode)
                        this.eleparent = el.parentNode;
                        if (binding.value) {
                            el.parentNode.appendChild(el);
                        } else {
                            el.parentNode.removeChild(el);
                        }
                    },
                    componentUpdated(el, binding, vnode) { //组件节点更新后
                        console.log(binding.value)
                        console.log(vnode)
                        if (binding.value) {
                            console.log(el)
                            this.eleparent.appendChild(el)
                        } else {
                            this.eleparent.removeChild(el)
                        }
                    }
                }
            }
        })
    </script>
</body>

</html>